package androidApplication;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import capstone.aw_capstone_androidapplication.R;
import fileService.CSFileWrapper;
import fileService.FileServiceClient;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class DeviceOverview extends Activity{
	private ListView lstHostFiles;
	private ListView lstTargetFiles;
	private ArrayList<String> targetFiles;
	private ArrayList<String> hostFiles;
//	private ArrayList<String> tempFiles;
	private boolean sourceHost = true;
	public DeviceOverview(){
		
	}
	public void onCreate(Bundle b){//
		super.onCreate(b);
		setContentView(R.layout.deviceoverview);
		
		targetFiles = getTargetFiles();
		hostFiles = getHostFiles();
		
		lstHostFiles = (ListView) findViewById(R.id.lstHostFiles);
		lstHostFiles.setAdapter(new BaseAdapter(){

			@Override
			public int getCount() {
				return hostFiles.size();
			}

			@Override
			public Object getItem(int position) {
				return hostFiles.get(position);
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View arg1, ViewGroup arg2) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
				View view = inflater.inflate(R.layout.list_row, null);
				TextView tview = (TextView) view.findViewById(R.id.TextView01);
				tview.setText(hostFiles.get(position));
				return view;
			}
			
		});
		registerForContextMenu(lstHostFiles);
		lstHostFiles.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				
					openContextMenu(lstHostFiles);
				return false;
			}
			
		});
		
		
		lstTargetFiles = (ListView) findViewById(R.id.lstTargetFiles);
		lstTargetFiles.setAdapter(new BaseAdapter(){

			@Override
			public int getCount() {
				return targetFiles.size();
			}

			@Override
			public Object getItem(int position) {
				return targetFiles.get(position);
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View arg1, ViewGroup arg2) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
				View view = inflater.inflate(R.layout.list_row, null);
				TextView tview = (TextView) view.findViewById(R.id.TextView01);
				tview.setText(targetFiles.get(position));
				return view;
			}
			
		});
		registerForContextMenu(lstTargetFiles);
		lstTargetFiles.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
					openContextMenu(lstTargetFiles);
				return false;
			}
			
		});
	}
	private ArrayList<String> getTargetFiles(){
		String ip = (String)getIntent().getSerializableExtra("target");
		ArrayList<String> fileNames = null;
		try {
			fileNames = new FileServiceClient().getFileNames(ip);
		} catch (UnknownHostException e) {e.printStackTrace();
		} catch (IOException e) {e.printStackTrace();
		}

		return fileNames;
	}
	private ArrayList<String> getHostFiles(){
		Commons common = (Commons)getIntent().getSerializableExtra("Commons");

		return common.getFileIO().getFileNames();
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo){
		if(view.getId() == R.id.lstTargetFiles){
//			tempFiles = targetFiles;
			sourceHost = false;
		}
		if(view.getId() == R.id.lstHostFiles){
//			tempFiles = hostFiles;
			sourceHost = true;
		}
		
		
		getMenuInflater().inflate(R.menu.device_overview_menu, menu);
	}
	@Override
	public boolean onContextItemSelected(MenuItem item){
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		Commons common = (Commons)getIntent().getSerializableExtra("Commons");
		String ip = (String)getIntent().getSerializableExtra("target");
		Intent intent;
		switch(item.getItemId()){
			case R.id.copy:
				if(sourceHost){
					if(info.position < hostFiles.size()){
						String fileName = hostFiles.get(info.position);
						CSFileWrapper file = null;
						
						file = common.getFileIO().getFile(fileName);
						
						try {
							new FileServiceClient().sendFile(file, ip);
						} catch (UnknownHostException e) {e.printStackTrace();
						} catch (IOException e) {e.printStackTrace();
						}
					}
				}else{
					if(info.position < targetFiles.size()){
						String fileName = targetFiles.get(info.position);
						CSFileWrapper file = null;
						try {
							file = new FileServiceClient().getFile(ip, fileName);
						} catch (UnknownHostException e) {e.printStackTrace();
						} catch (IOException e) {e.printStackTrace();
						}
						
						common.getFileIO().writeFile(file.getFileName(), file.getData());
					}
				}
				
				intent = new Intent(DeviceOverview.this.getApplication(), DeviceOverview.class);
				intent.putExtra("Commons", common);
				intent.putExtra("target", ip);
				startActivity(intent);
				break;
			case R.id.preview:
				if(sourceHost){
					if(info.position < hostFiles.size()){
						String fileName = hostFiles.get(info.position);
						
						intent = new Intent();
						intent.setAction(android.content.Intent.ACTION_VIEW);
						File fileT = new File(common.getFileIO().getExternalStorageDir()+common.getFileIO().getShareDir()+fileName);
						String fileMime = getFileMime(fileName);
						intent.setDataAndType(Uri.fromFile(fileT), fileMime);
						startActivity(intent);
					}
				}else{
					if(info.position < targetFiles.size()){
						String fileName = targetFiles.get(info.position);
						
						FileServiceClient client = new FileServiceClient();
						CSFileWrapper file = new CSFileWrapper();
						try {
							file = client.getFile(ip, fileName);
						} catch (UnknownHostException e) {e.printStackTrace();
						} catch (IOException e) {e.printStackTrace();
						}
						common.getFileIO().writePreviewFile(file.getFileName(), file.getData());
						
						
						intent = new Intent();
						intent.setAction(android.content.Intent.ACTION_VIEW);
						File fileT = new File(common.getFileIO().getExternalStorageDir()+common.getFileIO().getTempDir()+fileName);
						String fileMime = getFileMime(fileName);
						intent.setDataAndType(Uri.fromFile(fileT), fileMime);
						startActivity(intent);
					}
				}
				System.out.println("Preview");
				break;
		}
		
		return false;
	}
	
	private String getFileMime(String fileName){
		String mime = "*/*";
		String extension = "";
		int i = fileName.lastIndexOf('.');
		if(i > 0)
			extension = fileName.substring(i + 1);
		
		if(extension.equals("txt")){
			mime = "text/*";
		}else if(extension.equals("csv")){
			mime = "text/*";
		}else if(extension.equals("xml")){
			mime = "text/*";
		}else if(extension.equals("htm")){
			mime = "text/*";
		}else if(extension.equals("html")){
			mime = "text/*";
		}else if(extension.equals("php")){
			mime = "text/*";
		}else if(extension.equals("png")){
			mime = "image/*";
		}else if(extension.equals("gif")){
			mime = "image/*";
		}else if(extension.equals("jpg")){
			mime = "image/*";
		}else if(extension.equals("jpeg")){
			mime = "image/*";
		}else if(extension.equals("bmp")){
			mime = "image/*";
		}else if(extension.equals("mp3")){
			mime = "audio/*";
		}else if(extension.equals("wav")){
			mime = "audio/*";
		}else if(extension.equals("ogg")){
			mime = "audio/*";
		}else if(extension.equals("mid")){
			mime = "audio/*";
		}else if(extension.equals("midi")){
			mime = "audio/*";
		}else if(extension.equals("amr")){
			mime = "audio/*";
		}else if(extension.equals("mpeg")){
			mime = "video/*";
		}else if(extension.equals("3gp")){
			mime = "video/*";
		}else if(extension.equals("jar")){
			mime = "application/java-archive";
		}else if(extension.equals("zip")){
			mime = "application/zip";
		}else if(extension.equals("rar")){
			mime = "application/x-rar-compressed";
		}else if(extension.equals("gz")){
			mime = "application/gzip";
		}else if(extension.equals("apk")){
			mime = "application/vnd.android.package-archive";
		}else{
			Log.i("DeviceOverview", fileName);
			Log.i("DeviceOverview", extension);
			//not supported
		}
		
		return mime;
	}
}
